import moment from 'moment';
import {
  getNodeCountApi,
  getPartnerNodeApi,
  getTaskStatusApi,
  getOrderCountApi,
  getIncomeApi,
  getPartnerCountApi,
  getGoodsRankApi,
  getOrderRegionApi,
  getAmountApi,
} from "@/api/home";
import {
  dateRangeApi,
  dateRangeApiSec,
  dateRangeApiSecF,
  weekRangeApi,
  monthRangeApi,
  yearRangeApi
} from "@/utils/time";

const state = {
  nodeCount: "",
  partnerNode: [],
  partnerCount: "",
  income: "",
  ordercount: "",
  taskTotal: "",
  taskDone: "",
  taskPending: "",
  taskCanceled: "",
  goodsRank: {},
  chart1xAxis: [],
  chart1Series:[],
  chart2xAxis: [],
  chart2Series:[],
};

const mutations = {
  //获取当时工单汇总信息(人员统计头部信息)  done
  getTaskStatus(state, data) {
    state.taskTotal = data.reduce((sum, item) => sum + item.total, 0);
    state.taskDone = data.reduce((sum, item) => sum + item.completedTotal, 0);
    state.taskPending = data.reduce((sum, item) => sum + item.progressTotal, 0);
    state.taskCanceled = data.reduce((sum, item) => sum + item.cancelTotal, 0);
  },

  // 获取销售前几的商品  done
  getGoodsRank(state, data) {
    state.goodsRank = data;
  },

  // 获取点位总数  done
  getNodeCount(state, num) {
    state.nodeCount = num;
  },

  // 获取合作商点位状态  done
  getPartnerNode(state, info) {
    state.partnerNode = [...info];
    // console.log(state.partnerNode);
  },

  // 获取合作商总数  done
  getPartnerCount(state, num) {
    state.partnerCount = num;
  },

  // 获取半个月订单量  done
  getOrderCount(state, num) {
    state.ordercount = num;
  },

  // 获取半个月销售额  done
  getIncome(state, num) {
    const num1 = num / 1000000;
    state.income = num1.toFixed(2);
  },

  //获取销售额统计  图表1
  getAmount(state, data) {
    state.chart1xAxis = [...data.xAxis];
    let arr = []
    for (let i = 0; i < data.series.length; i++){
      arr[i] = (data.series[i] / 100).toFixed(0)
    }
    state.chart1Series = [...arr]
  },
  getAmountMonth(state, data) {
    let arr1 = data.xAxis.map(item => moment(item).format('M月D日'));
    state.chart1xAxis = [...arr1];
    let arr = []
    for (let i = 0; i < data.series.length; i++){
      arr[i] = (data.series[i] / 100).toFixed(0)
    }
    state.chart1Series = [...arr]
  },
  
  // 根据地区汇总销售额数据(销售量分布) 图表2
  getOrderRegion(state, data) {
    state.chart2xAxis = [...data.xAxis];
    let arr = []
    for (let i = 0; i < data.series.length; i++){
      arr[i] = (data.series[i] / 100).toFixed(0)
    }
    state.chart2Series = [...arr]
  },
};

const actions = {
  // 获取当时工单汇总信息(人员统计头部信息)  done
  async getTaskStatus({ commit }) {
    const res = await getTaskStatusApi(
      dateRangeApiSecF.start,
      dateRangeApiSecF.end
    );
    commit("getTaskStatus", res);
  },

  // 获取半个月的订单数 done
  async getOrderCount({ commit }) {
    const res = await getOrderCountApi(
      dateRangeApiSec.start,
      dateRangeApiSec.end
    );
    commit("getOrderCount", res);
  },

  // 获取半个月收入  done
  async getIncome({ commit }) {
    const res = await getIncomeApi(dateRangeApiSec.start, dateRangeApiSec.end);
    commit("getIncome", res);
  },

  // 获取销售前几的商品  done
  async getGoodsRank({ commit }) {
    const res = await getGoodsRankApi(10, dateRangeApi.start, dateRangeApi.end);
    commit("getGoodsRank", res);
  },

  // 获取销售额统计  图表1
  async getAmountWeek({ commit }) {
    const res = await getAmountApi(1, weekRangeApi.start, weekRangeApi.end)
    commit("getAmount", res);
  },
  async getAmountMonth({ commit }) {
    const res = await getAmountApi(1, monthRangeApi.start, monthRangeApi.end)
    commit("getAmountMonth", res);
  },
  async getAmountYear({ commit }) {
    const res = await getAmountApi(2, yearRangeApi.start, yearRangeApi.end)
    commit("getAmount", res);
  },

  // 根据地区汇总销售额数据(销售量分布) 图表2
  async getOrderRegionWeek({ commit }) {
    const res = await getOrderRegionApi(weekRangeApi.start, weekRangeApi.end);
    commit("getOrderRegion", res);
  },
  async getOrderRegionMonth({ commit }) {
    const res = await getOrderRegionApi(monthRangeApi.start, monthRangeApi.end);
    commit("getOrderRegion", res);
  },
  async getOrderRegionYear({ commit }) {
    const res = await getOrderRegionApi(yearRangeApi.start, yearRangeApi.end);
    commit("getOrderRegion", res);
  },

  // 获取点位总数 图表3 done
  async getNodeCount({ commit }) {
    const res = await getNodeCountApi();
    commit("getNodeCount", res);
  },

  // 获取合作商总数 图表3 done
  async getPartnerCount({ commit }) {
    const res = await getPartnerCountApi();
    commit("getPartnerCount", res);
  },

  // 获取合作商点位状态 图表3 done
  async getPartnerNode({ commit }) {
    const res = await getPartnerNodeApi();
    let total = 0;
    for (let i = 0; i < res.length; i++) {
      total += res[i].value;
    }
    let arr = [];
    res.forEach((item) => {
      let newNum = ((item.value / total) * 100).toFixed(2);
      arr.push({ name: item.name, value: newNum });
    });
    commit("getPartnerNode", arr);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
